<template>
  <div class="measure_wrap">
    <!-- <el-card class="measurement_wrap_card"> 度量管理 </el-card> -->
    <div class="measure_top_box">
      <el-card class="measure_top_card">
        <el-form :inline="true" class="demo-form-inline">
          <!-- 度量名称 -->
          <el-form-item label="度量名称">
            <el-input v-model="searchKeyw" placeholder="请输入度量名称" clearable style="width: 200px" />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" style="position: relative; left: -8px" @click="searchSelectClick">
              <el-icon style="margin-right: 6px"><Search /></el-icon>
              查询
            </el-button>
            <el-button type="primary" @click="addClick">
              <el-icon style="margin-right: 6px"><Plus /></el-icon>
              添加
            </el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
    <div class="measure_bottom_box">
      <el-card class="measure_bottom_card">
        <el-table :data="showMeasureList" border style="width: 100%" v-loading="isTableLoading" stripe>
          <!-- 序号 -->
          <el-table-column prop="serialNumber" label="序号" width="80" align="center" />

          <!-- 类别名称 -->
          <el-table-column prop="name" label="名称" align="center" show-overflow-tooltip />

          <!-- 创建者 -->
          <el-table-column prop="creator" label="创建者" align="center" show-overflow-tooltip />

          <!-- 更新者 -->
          <el-table-column prop="updator" label="更新者" align="center" show-overflow-tooltip />

          <!-- 操作 -->
          <el-table-column label="操作" align="center" width="120">
            <template #default="scope">
              <div class="operation_box">
                <!-- 删除 -->
                <el-popconfirm width="200" title="确认删除此度量吗?" @confirm="deleteMeasureFunc(scope.row)">
                  <template #reference>
                    <span class="del_text" style="color: red; cursor: pointer">删除</span>
                  </template>
                </el-popconfirm>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>

    <!-- 添加类别弹窗 -->
    <el-dialog v-model="isShowAddMeasureDialog" title="添加度量" top="8vh" width="460" draggable>
      <div class="add_form_box">
        <el-form
          label-position="right"
          label-width="100px"
          ref="addMeasureFormRef"
          :model="addMeasureDataFromData"
          :rules="addMeasureFormRules"
        >
          <!-- 度量名称 -->
          <el-form-item label="度量名称" prop="name">
            <el-input placeholder="请输入度量名称" v-model="addMeasureDataFromData.name" />
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="addMeasureDialogCancelClick">取消</el-button>
          <el-button type="primary" @click="addMeasureDialogConfirmClick(addMeasureFormRef)"> 确定 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, onMounted } from "vue";
import { ElMessage } from "element-plus";
export default defineComponent({
  name: "Measurement"
});
</script>
<script lang="ts" setup>
import { MeasureListType, MeasureType } from "@/api/modules/measure/type";
import { selectMeasureListApi, deleteMeasureApi, addMeasureApi } from "@/api/modules/measure/index";

// 添加度量点击事件
const addClick = () => {
  isShowAddMeasureDialog.value = true;
};

//  添加度量表单数据
const addMeasureDataFromData = ref({
  name: ""
});

// 添加度量表单弹窗取消点击事件
const addMeasureDialogCancelClick = () => {
  isShowAddMeasureDialog.value = false;
  clearAddMeasureData();
};

// 添加度量表单弹窗确定点击事件
const addMeasureDialogConfirmClick = async formRef => {
  if (!formRef) return;
  await formRef.validate((valid, fields) => {
    if (valid) {
      console.log("submit!");
      addMeasureFunc();
      isShowAddMeasureDialog.value = false;
      formRef.resetFields();
    } else {
      console.log("error submit!", fields);
    }
  });
};

// 添加度量请求函数
const addMeasureFunc = async () => {
  await addMeasureApi({
    name: addMeasureDataFromData.value.name
  })
    .then(res => {
      if (res.status == 400) {
        ElMessage.error(res.msg);
      } else {
        ElMessage.success("添加成功");
      }
    })
    .catch(err => {
      ElMessage.error("添加失败");
    })
    .finally(() => {
      selectMeasureListFunc();
    });
};

// 清空添加度量表单数据
const clearAddMeasureData = () => {
  addMeasureDataFromData.value.name = "";
};

// 添加度量表单Ref
const addMeasureFormRef = ref<any>(null);

// 添加度量表单校验规则
const addMeasureFormRules = ref({
  name: [{ required: true, message: "请输入药品名称", trigger: "blur" }]
});

// 显示添加度量弹窗的标识
const isShowAddMeasureDialog = ref(false);

// 搜索查询关键字
const searchKeyw = ref("");

// 搜索查询点击事件
const searchSelectClick = () => {
  if (searchKeyw.value == "") {
    showMeasureList.value = measureList.value;
  } else {
    showMeasureList.value = measureList.value.filter(item => {
      return item.name.indexOf(searchKeyw.value) != -1;
    });
  }
};

// 删除确定点击事件
const deleteMeasureFunc = async (row: MeasureType) => {
  await deleteMeasureApi({
    id: row.id
  })
    .then(res => {
      ElMessage.success("删除成功");
    })
    .catch(err => {
      ElMessage.success("删除失败");
    })
    .finally(() => {
      selectMeasureListFunc();
    });
};

// 度量列表数据
const measureList = ref<MeasureListType>([]);

// 需要显示的度量列表数据
const showMeasureList = ref<MeasureListType>([]);

// 表格加载标识
const isTableLoading = ref(false);

// 查询度量列表的函数
const selectMeasureListFunc = async () => {
  isTableLoading.value = true;
  await selectMeasureListApi()
    .then(res => {
      res.data.forEach((item, indexM) => {
        item.serialNumber = indexM + 1;
      });
      measureList.value = res.data;
      showMeasureList.value = res.data;
    })
    .finally(() => {
      isTableLoading.value = false;
    });
};

// init
onMounted(() => {
  selectMeasureListFunc();
});
</script>

<style lang="scss" scoped>
.measure_wrap {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  .measure_top_box {
    width: 100%;
    height: 70px;
    .measure_top_card {
      width: 100%;
      height: 100%;
    }
  }
  .measure_bottom_box {
    flex: 1;
    width: 100%;
    margin-top: 14px;
    .measure_bottom_card {
      width: 100%;
      height: 100%;
      .measure_list_box {
        display: flex;
        flex-direction: column;
        width: 600px;
        height: 680px;
        border: 1px solid red;
        .measure_item_box {
          display: flex;
          flex-direction: row;
          justify-content: space-between;
          padding: 6px 10px;
        }
      }
    }
  }
  .add_form_box {
    padding-top: 40px;
    padding-right: 20px;
    padding-bottom: 20px;
  }
}
</style>
